package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.MemberDao;
import com.mstore.pojo.Member;

/**
 * 
 * 会员信息
 * 
 */
public class MemberService {

	private MemberDao memberDao;

	/**
	 * 
	 * 添加会员记录
	 */
	public void addMember(Member member) {
		memberDao.save(member);
	}

	/**
	 * 修改会员
	 */
	public void updateMember(Member member) {
		memberDao.update(member);
	}

	/**
	 * 
	 * 通过type查询企业下的会员
	 */
	public List<Object> getAllMember(Member member) {
		return memberDao.getAllMember(member);
	}

	/**
	 * 
	 * 分页查询
	 */
	public List<Member> getMemberPage(int index, Member member) {
		List<Object> objects = memberDao.getMemberPage(index, member);
		List<Member> members = new ArrayList<Member>();
		for (Object object : objects) {
			Member mb = (Member) object;
			mb.setCreateTime(mb.getCreateTime().substring(0,
					mb.getCreateTime().indexOf(".")));
			members.add((Member) object);
		}
		return members;
	}

	/**
	 * 
	 * 获取全部条数
	 */
	public int getMemberLine(Member member) {
		return memberDao.getMemberLine(member);
	}

	/**
	 * 
	 * 验证企业手机号的唯一性
	 */
	public boolean checkActMobile(Member member) {
		return memberDao.checkActMobile(member);
	}
	
	/**
	 * 验证手机号的唯一性
	 */
	public boolean checkMobile(Member member) {
		return memberDao.checkMobile(member);
	}

	/**
	 * 验证手机号的唯一性
	 */
	public boolean checkMobile1(Member member) {
		return memberDao.checkMobile1(member);
	}
	
	/**
	 * 验证昵称的唯一性
	 */
	public boolean checkNickName(Member member) {
		return memberDao.checkNickName(member);
	}
	
	/**
	 * 
	 * 查询关注状态的条数
	 */
	public int getMemberLineBytype(Member member) {
		return memberDao.getMemberLineBytype(member);
	}

	public Object getOneMember(Member member) {
		Object object = memberDao.getOneMember(member);
		if (object == null) {
			return null;
		}
		Member mb = (Member) object;
		mb.setCreateTime(mb.getCreateTime().substring(0,
				mb.getCreateTime().indexOf(".")));
		return mb;
	}

	public Member getTheMember(Member member) {
		Object object = memberDao.getOneMember(member);
		if (object == null) {
			return null;
		}
		return (Member) object;
	}

	public Object getOneMemberByBalance(Long id) {
		return memberDao.getOneMemberByBalance(id);
	}

	/**
	 * 通过手机号查询一个会员
	 */
	public Member getmemberByMobile(Member member) {
		Object object = memberDao.getmemberByMobile(member);
		if (object == null) {
			return null;
		}
		return (Member) object;
	}
	
	/**
	 * 通过手机号查询一个会员
	 */
	public Member getMemberByMobileAndType(Member member) {
		Object object = memberDao.getMemberByMobileAndType(member);
		if (object == null) {
			return null;
		}
		return (Member) object;
	}

	public Member getMemberById(Member member) {
		Object object = memberDao.getMemberById(member);
		if (object == null) {
			return null;
		}
		return (Member) object;
	}

	/**
	 * 
	 * 删除会员
	 */
	public void delMember(Member member) {
		memberDao.delMember(member);
	}

	/**
	 * 验证邮箱
	 * 
	 * @return
	 */
	public List<Object> getMemberByEmail(Member member) {
		List<Object> objects = memberDao.getMemberByEmail(member);
		return objects;
	}
	
	/**
	 * 验证邮箱但排除自己
	 */
	public List<Object> getMemberByEmail1(Member member) {
		List<Object> objects = memberDao.getMemberByEmail1(member);
		return objects;
	}

	/**
	 * 通过手机号/邮箱/昵称和密码查询一名会员
	 */
	public Member checkMember(String loginName, String password) {
		return memberDao.checkMember(loginName, password) != null ? (Member) memberDao
				.checkMember(loginName, password) : null;
	}
	/**
	 * 更新邮箱
	 * @return
	 */
	public void updateEmail(Member member){
		memberDao.updateEmail(member);
	}
	
	/**
	 * 修改密码根据eaid
	 * @return
	 */
	public void updataPasswordByEaId(Member member){
		memberDao.updataPasswordByEaId(member);
	}
	
	/**
	 * 检查电话号码
	 */
	public List<Member> checkMobile(String mobile){
		
		List<Object> objects=memberDao.checkMobile(mobile);
		List<Member> list=new ArrayList<Member>();
		for(int i=0;i<objects.size();i++){
			Member m=(Member) objects.get(i);
			list.add(m);
		}
		return list;
	}
	/**
	 * 根据邮箱修改密码
	 * @return
	 */
	public void updataPasswordByEmail(Member member){
		memberDao.updataPasswordByEmail(member);
	}
	
	/**
	 * 通过邮箱查找
	 * @return
	 */
	public List<Member> findByEmail(Member member){
		List<Object> objects=memberDao.findByEmail(member);
		List<Member> list=new ArrayList<Member>();
		for(int i=0;i<objects.size();i++){
			Member m=(Member) objects.get(i);
			list.add(m);
		}
		return list;
	}
	
	/**
	 * 通过手机查找
	 * @return
	 * 
	 */
	public List<Member> findByMobile(Member member) {
		List<Object> objects=memberDao.findByMobile(member);
		List<Member> list=new ArrayList<Member>();
		for(int i=0;i<objects.size();i++){
			Member m=(Member) objects.get(i);
			list.add(m);
		}
		return list;
	}
	
	/**
	 * 通过手机查找
	 * @return
	 * 
	 */
	public Member findOneByMobile(Member member) {
		List<Object> objects=memberDao.findOneByMobile(member);
		if (objects.isEmpty()) {
			return null;
		}
		return (Member)objects.get(0);
	}
	
	/**
	 * 条件查询数量，排除自身
	 */
	public int getCountFromMemberByConditionExceptSelf(Member member) {
		return memberDao.getCountFromMemberByConditionExceptSelf(member);
	}
	
	public MemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

}
